--#include "data\config\activityconfig\CrossActivity\CrossEatChickenConfig.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理

CROSS_EATCHICKEN_IDX   			= 0 				--此副本是第几个擂台
CROSS_EATCHICKEN_GIFT_IDX   	= 1 				--第几次空投
CROSS_EATCHICKEN_EXPLODE_IDX   	= 2 				--第几次刷自爆
CROSS_EATCHICKEN_TOXIC_IDX   	= 3 				--第几次中毒区域
CROSS_EATCHICKEN_TOXIC_NEXT   	= 4 				--下次中毒区域
CROSS_EATCHICKEN_TOXIC_TIME		= 5 				--下次中毒时间
CROSS_EATCHICKEN_BUFF_IDX		= 6 				--下次buff加深
CROSS_EATCHICKEN_EXPLODE_TIME   = 7 				--下次刷自爆时间
CROSS_EATCHICKEN_EXGIFT_IDX		= 8					--第几次额外补给

--获取数据
function CrossEatChickenGetRank(sysarg, packet)
	----[[
	if not Actor.IsCross(sysarg) then
		return
	end
	--]]
	SendCrossEatChickenDataSub(sysarg)
end

function SendCrossEatChickenDataSub(sysarg)
	local rankPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_RANK].strName)
	if not rankPtr then return end
	local rankSignPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_SIGN].strName)
	if not rankSignPtr then return end
	local actorid = Actor.getActorId(sysarg)
	local myItem = Ranking.getItemPtrFromId(rankPtr, actorid)
	local myRank = 0
	local myScore = 0
	local myAwardState = 0
	local mySignState = 0
	local dailyCount = 0
	local myDropPoint = 1
	local curSignCount = 0
	if myItem then
		myRank = Ranking.getIndexFromPtr(myItem) + 1
		myScore = Ranking.getPoint(myItem)
		if myScore < CrossEatChickenCfg.rankLimit then
			myRank = 0
		elseif myRank > CrossEatChickenCfg.RankNum then
			myRank = 0
		end
		
		local data = Ranking.getSub(myItem, 1)
		local myData
		if not data or data == "" then
			myData = {}
		else
			myData = BaseFuc_unserialize(data)
		end

		if not myData.dailyCount then
			myData.dailyCount = 0
		end

		dailyCount = myData.dailyCount

		
		if ( not myData.myAwardState or myData.myAwardState == 0 ) and IsCrossEatChickenCanGetAward() then
			myAwardState = 1
		else
			myAwardState = myData.myAwardState or 0
		end

		if not myData.myDropPoint then
			myData.myDropPoint = 1
		end

		myDropPoint = myData.myDropPoint
	end

	if not IsCrossEatChickenCanSign() then
		mySignState = 0
	else
		local sign = gRankMgr_GetValue(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
		if sign > 0 then
			mySignState = 2
		else
			mySignState = 1
		end
	end

	local RankCount = Ranking.getRankItemCountByLimit(rankPtr, CrossEatChickenCfg.rankLimit)
	if RankCount > CrossEatChickenCfg.RankNum then
		RankCount = CrossEatChickenCfg.RankNum
	end

	curSignCount = Ranking.getRankItemCount( rankSignPtr )

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenData) 
  	if not netPack then return end
	--print("myRank = "..myRank)
  	DataPack.writeShort(netPack, myRank or 0)
  	--print("myScore = "..myScore)
  	DataPack.writeInt(netPack, myScore or 0)
  	--print("myAwardState = "..myAwardState)
  	DataPack.writeByte(netPack, myAwardState or 0)
  	--print("dailyCount = "..dailyCount)
  	DataPack.writeByte(netPack, dailyCount or 0)
  	--print("curSignCount = "..curSignCount)
  	DataPack.writeByte(netPack, curSignCount or 0)
  	--print("myDropPoint = "..myDropPoint)
  	DataPack.writeByte(netPack, myDropPoint or 1)
  	--print("mySignState = "..mySignState)
  	DataPack.writeByte(netPack, mySignState or 0)

  	DataPack.writeByte(netPack, RankCount)
  	if RankCount > 0 then
		for idx = 1, RankCount do
			local item = Ranking.getItemFromIndex(rankPtr, idx-1)
			if item then
				local actorid = tonumber(Ranking.getId(item))
				local point = Ranking.getPoint(item)
				local data = Ranking.getSub(item, 1)
				local dataTable
				if not data or data == "" then
					dataTable = {}
				else
					dataTable = BaseFuc_unserialize(data)
				end
				DataPack.writeString(netPack, dataTable.name or "")
				--print("dataTable.name = "..dataTable.name)
				DataPack.writeInt(netPack, point or 0)
				--print("point = "..point)
			end
		end
	end
	local sVar = System.getStaticVar()
	local startNum = sVar.szEatChickenStartCount or CrossEatChickenCfg.openGameCount
	DataPack.writeByte(netPack, startNum)
  	DataPack.flush(netPack)
end

--报名
function CrossEatChickenSign(sysarg, packet)
	----[[
	if not Actor.IsCross(sysarg) then
		return
	end
	--]]
	local opType = DataPack.readByte(packet)
	CrossEatChickenSignSub(sysarg, opType)
end

function CrossEatChickenSignSub(sysarg, opType)
	--print("CrossEatChickenSignSub  opType ="..opType)
	if opType == 2 then
		if not IsCrossEatChickenCanSign() then
			return
		end
		local actorid = Actor.getActorId(sysarg)
		local sign = gRankMgr_GetValue(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
		if sign > 0 then
			gRankMgr_RemoveItemById(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
			SendCrossEatChickenSignStateChange(sysarg, 1, 0)

			CrossEatChickenNoticeSomeOneLeftSign()

			local rankSignPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_SIGN].strName)
			if not rankSignPtr then return end
			local signCount = Ranking.getRankItemCount( rankSignPtr )
			NoticeEatChickenCount(sysarg, signCount)
		end
	else
		if not IsCrossEatChickenCanSign() then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken003, ttFlyTip)
			return
		end

		local actorid = Actor.getActorId(sysarg)

		local sign = gRankMgr_GetValue(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
		if sign > 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken004, ttFlyTip)
			return
		end

		local levelLimit = CrossEatChickenCfg.levelLimit
		if not Actor.checkActorLevel( sysarg, levelLimit[2], levelLimit[1] ) then
	    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016,ttFlyTip)
	    	return
	    end

		local data = gRankMgr_GetValueById(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1)
		local myData = BaseFuc_unserialize(data)

		if not myData then
			myData = {}
		end
		
		myData.name = Actor.getName(sysarg)
		myData.level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
		myData.power = Actor.getIntProperty(sysarg, PROP_ACTOR_BATTLE_POWER)

		if not myData.dailyCount then
			myData.dailyCount = 0
		end

		if myData.dailyCount and myData.dailyCount >= CrossEatChickenCfg.dailyCount then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken013,ttFlyTip)
			return
		end

	    if not myData.sex then
	        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
	        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
	    end

		local newData = BaseFuc_serialize(myData)
		gRankMgr_SetRankEx(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1, newData)

		local score = gRankMgr_GetValue(actorid, RANK_CROSS_EAT_CHICKEN_RANK)

		gRankMgr_SetRank(actorid, RANK_CROSS_EAT_CHICKEN_SIGN, score + 1)
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken005, ttFlyTip)

		SendCrossEatChickenSignStateChange(sysarg, 2, 0)

		local rankSignPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_SIGN].strName)
		if not rankSignPtr then return end
		local signCount = Ranking.getRankItemCount( rankSignPtr )
		CrossEatChickenNoticeSomeOneLeftSign()
		--print("signCount ="..signCount)

		local sVar = System.getStaticVar()
		local startNum = sVar.szEatChickenStartCount or CrossEatChickenCfg.openGameCount
		if signCount >= startNum then
			CrossEatChickenStart()
		end
	end
end

--设置区域
function CrossEatChickenSettle(sysarg, packet)
	----[[
	if not Actor.IsCross(sysarg) then
		return
	end
	--]]
	local index = DataPack.readByte(packet)
	CrossEatChickenSettleSub(sysarg, index)
end

function CrossEatChickenSettleSub(sysarg, index)
	if IsInCrossEatChickenFuben(sysarg) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken016, ttFlyTip)
		return
	end
	if index < 1 or index > #CrossEatChickenCfg.Range then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken015, ttFlyTip)
		return
	end
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData then
		myData = {}
	end
	
	myData.name = Actor.getName(sysarg)
	myData.level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	myData.power = Actor.getIntProperty(sysarg, PROP_ACTOR_BATTLE_POWER)

	if not myData.dailyCount then
		myData.dailyCount = 0
	end

	myData.myDropPoint = index

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

	local newData = BaseFuc_serialize(myData)
	gRankMgr_SetRankEx(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1, newData)

	SendCrossEatChickenSettleChange(sysarg, index)
end


--领取奖励
function CrossEatChickenGetAward(sysarg, packet)
	----[[
	if not Actor.IsCross(sysarg) then
		return
	end
	--]]
	CrossEatChickenGetAwardSub(sysarg)
end

function CrossEatChickenGetAwardSub(sysarg)
	if not IsCrossEatChickenCanGetAward() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken007, ttFlyTip)
		return
	end

	local rankPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_RANK].strName)
	local actorid = Actor.getActorId(sysarg)
	local myItem = Ranking.getItemPtrFromId(rankPtr, actorid)
	if myItem then
		local myRank = Ranking.getIndexFromPtr(myItem) + 1
		local myScore = Ranking.getPoint(myItem)
		if myScore < CrossEatChickenCfg.rankLimit then
			myRank = 0
		elseif myRank > CrossEatChickenCfg.RankNum then
			myRank = 0
		end

		local myRewards = GetElemByCond( CrossEatChickenCfg.Awards, myRank )
		if not myRewards then
			--myRewards = CrossEatChickenCfg.rankAwards[#CrossEatChickenCfg.rankAwards]
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken014, ttFlyTip)
			return
		end

		--[[
		local days = System.getDaysSinceOpenServer()

		local myAwards = GetElemByCond( myRewards.awards, days )
		if not myAwards then
			myAwards = myRewards.awards[#myRewards.awards]
		end
		
		local myAward = myAwards.award
		]]
		local myAward = myRewards.award
		

		local actorid = tonumber(Ranking.getId(myItem))
		local data = Ranking.getSub(myItem, 1)
		local myData
		if not data or data == "" then
			myData = {}
		else
			myData = BaseFuc_unserialize(data)
		end
		if myData.myAwardState and myData.myAwardState == 2 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken011, ttFlyTip)
			return
		end

		if IsBagGridEnough(sysarg, myAward) then
			myData.myAwardState = 2
			local newData = BaseFuc_serialize(myData)
			gRankMgr_SetRankEx(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1, newData)
			GiveCommonAward(sysarg, myAward, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken)

			SendCrossEatChickenRewardStateChange(sysarg, 2, myRank, myScore)
		else
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0015, ttFlyTip)
		end
	end
end

--战斗过程广播
function NoticeCrossEatChickenWar(sysarg, kill, left, range, toxicTime, explodeTime)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenNotice) 
  	if not netPack then return end
  	--print("kill ="..kill)
  	DataPack.writeByte(netPack, kill or 0)
  	--print("left ="..left)
  	DataPack.writeByte(netPack, left or 0)
  	--print("range ="..range)
  	DataPack.writeByte(netPack, range or 0)
  	--print("toxicTime ="..toxicTime)
  	DataPack.writeShort(netPack, toxicTime or 0)
  	--print("explodeTime ="..explodeTime)
  	DataPack.writeShort(netPack, explodeTime or 0)
  	DataPack.flush(netPack)
end

--广播中毒区域
function NoticeCrossEatChickenToxic(sysarg, toxicData)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenToxic) 
	if not netPack then return end
	if not toxicData then
		toxicData = {}
	end
	local len  = #toxicData
  	--print("len ="..len)
  	DataPack.writeByte(netPack, len)
  	if len > 0 then
	  	for idx = 1 , len do
	  		DataPack.writeByte(netPack, toxicData[idx] or 0)
	  		--print("idx = "..idx..", toxicData[idx] ="..(toxicData[idx] or 0))
	  	end
  	end
  	DataPack.flush(netPack)
end

--广播报名人数
function NoticeEatChickenCount(sysarg, count)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenCount) 
	if not netPack then return end
  	--print("count ="..count)
  	DataPack.writeByte(netPack, count or 0)
  	DataPack.flush(netPack)
end

--设置改变
function SendCrossEatChickenSettleChange(sysarg, index)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenSettle) 
  	if not netPack then return end
  	--print("index ="..index)
  	DataPack.writeByte(netPack, index or 1)
  	DataPack.flush(netPack)
end

--报名状态改变
function SendCrossEatChickenSignStateChange(sysarg, state, needTip)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenSign) 
  	if not netPack then return end
  	--print("state ="..state)
  	DataPack.writeByte(netPack, state or 0)
  	--print("needTip ="..needTip)
  	DataPack.writeByte(netPack, needTip or 0)
  	DataPack.flush(netPack)
end

--战斗结束通知
function SendCrossEatChickenResult(sysarg, rank, kill, score)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenResult) 
  	if not netPack then return end
  	--print("rank ="..rank)
  	DataPack.writeByte(netPack, rank or 0)
  	--print("kill ="..kill)
  	DataPack.writeByte(netPack, kill or 0)
  	--print("score ="..score)
  	DataPack.writeInt(netPack, score or 0)
  	DataPack.flush(netPack)
end

--奖励状态改变
function SendCrossEatChickenRewardStateChange(sysarg, state, myRank, myScore)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCrossEatChickenAward) 
  	if not netPack then return end
  	--print("myRank ="..myRank)
  	DataPack.writeShort(netPack, myRank or 0)
  	--print("myScore ="..myScore)
  	DataPack.writeInt(netPack, myScore or 0)
  	--print("state ="..state)
  	DataPack.writeByte(netPack, state or 0)
  	DataPack.flush(netPack)
end

--是否在副本中
function IsInCrossEatChickenFuben(sysarg)
	--print("Actor.getFubenId( sysarg ) ="..Actor.getFubenId( sysarg ))
	if Actor.getFubenId( sysarg ) == CrossEatChickenCfg.fubenId then
		return true
	end
	return false
end
	
--活动是否开始
function IsCrossEatChickenStart()
	local sVar = System.getStaticVar()
	if sVar.CrossEatChickenStart == 1 then
		return true
	end
	return false
end

--是否可以报名
function IsCrossEatChickenCanSign()
	local sVar = System.getStaticVar()
	if sVar.CrossEatChickenSign == 1 then
		return true
	end
	return false
end

--是否可以领取奖励
function IsCrossEatChickenCanGetAward()
	local sVar = System.getStaticVar()
	if sVar.CrossEatChickenAward == 1 then
		return true
	end
	return false
end

--活动开始，清除旧数据
function CrossEatChickenClear()
	local week = System.getDayOfWeek()
	local sVar = System.getStaticVar()
	sVar.CrossEatChickenAward = nil
	CrossEatChickenResetCount()
	gRankMgr_Clear(RANK_CROSS_EAT_CHICKEN_SIGN)
	if week == 4 then
		gRankMgr_Clear(RANK_CROSS_EAT_CHICKEN_RANK)
		
		sVar.CrossEatChickenStart = 1
		if Lang.ScriptTips.CrossEatChicken006 ~= "" then
			System.broadcastTipmsg( Lang.ScriptTips.CrossEatChicken006, ttScreenCenter + ttChatWindow )
		end
	elseif not sVar.CrossEatChickenStart then
		sVar.CrossEatChickenStart = 1
		if Lang.ScriptTips.CrossEatChicken006 ~= "" then
			System.broadcastTipmsg( Lang.ScriptTips.CrossEatChicken006, ttScreenCenter + ttChatWindow )
		end
	end

	local dVar = System.getDyanmicVar()
	dVar.crossEatChicken = {}
end

--开启报名并广播
function CrossEatChickenSignBroad()
	if not IsCrossEatChickenStart() then return end
	if Lang.ScriptTips.CrossEatChicken001 ~= "" then
		System.broadcastTipmsg( Lang.ScriptTips.CrossEatChicken001, ttScreenCenter + ttChatWindow )
	end
	local sVar = System.getStaticVar()
	sVar.CrossEatChickenSign = 1
end

--结束报名并广播
function CrossEatChickenSignClose()
	if not IsCrossEatChickenStart() then return end
	if Lang.ScriptTips.CrossEatChicken002 ~= "" then
		System.broadcastTipmsg( Lang.ScriptTips.CrossEatChicken002, ttScreenCenter + ttChatWindow )
	end
	local sVar = System.getStaticVar()
	sVar.CrossEatChickenSign = nil
end

--重置次数
function CrossEatChickenResetCount()
	if not IsCrossEatChickenStart() then return end
	local rankPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_RANK].strName)
	if not rankPtr then return end
	local rankCount = Ranking.getRankItemCount(rankPtr)
	if rankCount > 0 then
		for idx = 1, rankCount do
			local item = Ranking.getItemFromIndex(rankPtr, idx-1)
			if item then
				local actorid = tonumber(Ranking.getId(item))
				local data = Ranking.getSub(item, 1)
				local myData
				if not data or data == "" then
					myData = {}
				else
					myData = BaseFuc_unserialize(data)
				end
				
				myData.dailyCount = 0

				local newData = BaseFuc_serialize(myData)
				gRankMgr_SetRankEx(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1, newData)
			end
		end
	end
end

--奖励可以领取
function CrossEatChickenCalc()
	if not IsCrossEatChickenStart() then return end
	local sVar = System.getStaticVar()
	sVar.CrossEatChickenStart = nil
	sVar.CrossEatChickenAward = 1
	sVar.CrossEatChickenSign = nil
	System.broadcastTipmsg( Lang.ScriptTips.CrossEatChicken012, ttScreenCenter + ttChatWindow )
	gRankMgr_Clear(RANK_CROSS_EAT_CHICKEN_SIGN)
	local rankPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_RANK].strName)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	for idx = 1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, idx-1)
		if item then
			local actorid = tonumber(Ranking.getId(item))
			local player = Actor.getActorById(actorid)
			if player then
				local myState = 1
				local score = Ranking.getPoint(item)
				local myRank = idx
				if score < CrossEatChickenCfg.rankLimit then
					myRank = 0
					myState = 0
				elseif myRank > CrossEatChickenCfg.RankNum then
					myRank = 0
					myState = 0
				end
				SendCrossEatChickenRewardStateChange(player, myState, myRank, score)
			end
		end
	end
end

--联赛pk正式开始
function CrossEatChickenStart()
	if not IsCrossEatChickenStart() then return end
	local rankPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_SIGN].strName)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	--print("rankCount ="..rankCount)
	local dVar = System.getDyanmicVar()
	if not dVar.crossEatChicken then
		dVar.crossEatChicken = {}
	end
	local index = 1
	while dVar.crossEatChicken[index] do
		index = index + 1
	end
	local fubenHandle = Fuben.createFuBen( CrossEatChickenCfg.fubenId )
	--print("fubenHandle = "..fubenHandle)
	local fubenPtr = Fuben.getFubenPtr( fubenHandle )
	dVar.crossEatChicken[index] = fubenHandle
	Fuben.setActivityType( fubenPtr, enFubenActivityType_CrossEatChicken )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_IDX, index )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_GIFT_IDX, 0 )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_IDX, 0 )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_NEXT, 0 )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_TIME, 0 )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_BUFF_IDX, 0 )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_EXGIFT_IDX, 0 )
	
	if not dVar.crossEatChickenToxic then
		dVar.crossEatChickenToxic = {}
	end
	dVar.crossEatChickenToxic[index] = {}

	local hScene = Fuben.getSceneHandleById(CrossEatChickenCfg.sceneId, fubenHandle)
	for ii = 1, #CrossEatChickenCfg.Range do
		Fuben.setSceneAreaAttri(hScene, CrossEatChickenCfg.Range[ii].rangeId, aaRunNpc, tostring(CrossEatChickenCfg.Range[ii].rangeId), "OnCrossEatChickenEnterArea", Actor.getHandle(System.getGlobalNpc()))
	end
	
	local time = System.getCurrMiniTime()
	--Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropItem")			--掉补给
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenExplode")				--自爆怪
	--Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicArea")			--毒圈
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicBuff")			--中毒buff
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropExItem")			--特殊掉补给

	--Fuben.regScriptTimer2S(fubenPtr, 0, CrossEatChickenCfg.giftMonsterCd[1] * 1000, CrossEatChickenCfg.giftMonsterCd[1] * 1000, -1, "OnCrossEatChickenDropItem")
	Fuben.regScriptTimer2S(fubenPtr, 0, (CrossEatChickenCfg.explodeMonstersCd[1] or 1) * 1000, (CrossEatChickenCfg.explodeMonstersCd[1] or 1) * 1000, -1, "OnCrossEatChickenExplode")
	--Fuben.regScriptTimer2S(fubenPtr, 0, CrossEatChickenCfg.toxicRangeCd[1] * 1000, CrossEatChickenCfg.toxicRangeCd[1] * 1000, -1, "OnCrossEatChickenToxicArea")
	Fuben.regScriptTimer2S(fubenPtr, 0, (CrossEatChickenCfg.toxicBuffCd[1] or 1) * 1000, (CrossEatChickenCfg.toxicBuffCd[1] or 1) * 1000, -1, "OnCrossEatChickenToxicBuff")
	Fuben.regScriptTimer2S(fubenPtr, 0, (CrossEatChickenCfg.exGiftMonsterCd[1] or 1) * 1000, (CrossEatChickenCfg.exGiftMonsterCd[1] or 1) * 1000, -1, "OnCrossEatChickenDropExItem")

	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_IDX, 0 )
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_TIME, time +  (CrossEatChickenCfg.explodeMonstersCd[1] or 1))

	OnCrossEatChickenDropItem(fubenPtr)
	OnCrossEatChickenToxicArea(fubenPtr)

	for idx = 1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
		if item then		--当前榜
			local playerId  = tonumber(Ranking.getId(item))
			--print("playerId ="..playerId)
			local player 	= Actor.getActorById( playerId )
			if player then 		--玩家必须在线
				if Actor.canBeTelported( player ) == enCanntBeTeledReason_No then			--玩家能够被拉传
					local data = gRankMgr_GetValueById(playerId, RANK_CROSS_EAT_CHICKEN_RANK, 1)
					local myData = BaseFuc_unserialize(data)

					if not myData then
						myData = {}
					end
					
				    if not myData.myDropPoint then
				    	myData.myDropPoint = 1
				    end
				    local config = CrossEatChickenCfg.Range[myData.myDropPoint]
				    local ranPoint = System.getRandomNumber(#config.rangeEnterPoint) + 1
				    local myPoint = config.rangeEnterPoint[ranPoint]
					if Actor.enterFuBen( player, fubenHandle, CrossEatChickenCfg.sceneId, myPoint[1], myPoint[2]) then
						OpenCrossEatChickenPanel( player )
						if not myData.name then
							myData.name = Actor.getName(player)
							myData.level = Actor.getIntProperty(player, PROP_CREATURE_LEVEL)
							myData.power = Actor.getIntProperty(player, PROP_ACTOR_BATTLE_POWER)
						end
						if not myData.sex then
					        myData.sex = Actor.getIntProperty(player, PROP_ACTOR_SEX)
					        myData.vocation = Actor.getIntProperty(player, PROP_ACTOR_VOCATION)
					    end
						if not myData.dailyCount then
							myData.dailyCount = 0
						end
						myData.dailyCount = 1 + myData.dailyCount
						myData.myKillNumber = 0
						local newData = BaseFuc_serialize(myData)
						gRankMgr_SetRankEx(playerId, RANK_CROSS_EAT_CHICKEN_RANK, 1, newData)

						local ActVar = Actor.getDyanmicVar(player)
						local hp = Actor.getIntProperty(player,PROP_CREATURE_HP)
						local mp = Actor.getIntProperty(player,PROP_CREATURE_MP)
						ActVar.EnterCrossEatChickenHP = hp
						ActVar.EnterCrossEatChickenMP = mp

						AddCrossEatChickenBuff(player)

						ActorHpMpInCrossEatChicken(player)
					end
				end
			end
		end
	end

	gRankMgr_Clear(RANK_CROSS_EAT_CHICKEN_SIGN)
	if Lang.ScriptTips.CrossEatChicken009 ~= "" then
		System.broadcastTipmsg( Lang.ScriptTips.CrossEatChicken009, ttScreenCenter + ttChatWindow )
	end
end

--进入场景
function CrossEatChickenEnterSceneHandler( sysarg, sceneId, lastSceneId )
	if not IsCrossEatChickenCanSign() then
		return
	end
	local actorid = Actor.getActorId(sysarg)
	local sign = gRankMgr_GetValue(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
	if sign > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken008, ttFlyTip)
		gRankMgr_RemoveItemById(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
		CrossEatChickenNoticeSomeOneLeftSign()
		return
	end
end

--广播匹配人数
function CrossEatChickenNoticeSomeOneLeftSign()
	local rankSignPtr = Ranking.getRanking(RankList[RANK_CROSS_EAT_CHICKEN_SIGN].strName)
	if not rankSignPtr then return end
	local rankCount = Ranking.getRankItemCount( rankSignPtr )
	--print("rankCount ="..rankCount)
	if rankCount and rankCount > 0 then
		for idx = 1, rankCount do
			local item = Ranking.getItemFromIndex(rankSignPtr, idx-1)
			if item then
				local actorid = tonumber(Ranking.getId(item))
				local player = Actor.getActorById(actorid)
				if player then
					NoticeEatChickenCount(player, rankCount)
				end
			end
		end
	end
end

--离线
function CrossEatChickenOffLine(sysarg)
	if not IsCrossEatChickenCanSign() then
		return
	end
	local actorid = Actor.getActorId(sysarg)
	local sign = gRankMgr_GetValue(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
	if sign > 0 then
		gRankMgr_RemoveItemById(actorid, RANK_CROSS_EAT_CHICKEN_SIGN)
		CrossEatChickenNoticeSomeOneLeftSign()
		return
	end
end

--进入或者离开毒区
function OnCrossEatChickenEnterArea(sysarg)
	local index = Actor.getMapAttriValue(sysarg, aaRunNpc)
	--print("OnCrossEatChickenEnterArea index ="..(index or -1))
	local fubenPtr = Actor.getFubenPrt( sysarg )
	local fubenIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_IDX )
	local dVar = System.getDyanmicVar()
	if not dVar.crossEatChickenToxic then
		dVar.crossEatChickenToxic = {}
	end
	if not dVar.crossEatChickenToxic[fubenIdx] then
		dVar.crossEatChickenToxic[fubenIdx] = {}
	end
	local needAddBuff = false
	for idx = 1, #dVar.crossEatChickenToxic[fubenIdx] do
		if dVar.crossEatChickenToxic[fubenIdx][idx] == tonumber(index) then
			needAddBuff = true
			local buffIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_BUFF_IDX ) + 1
			--print("OnCrossEatChickenEnterArea addbuff buffIdx ="..buffIdx)
			local buffId = CrossEatChickenCfg.toxicBuffId[buffIdx]
			if not buffId then
				buffId = CrossEatChickenCfg.toxicBuffId[#CrossEatChickenCfg.toxicBuffId]
			end
			if not Actor.hasBuffById(sysarg, buffId) then
				Actor.addBuffById(sysarg, buffId)
			end
		end
	end
	if not needAddBuff then
		for i, buffId in ipairs(CrossEatChickenCfg.toxicBuffId) do
			if Actor.hasBuffById(sysarg, buffId) then
				Actor.delBuffById(sysarg, buffId)
			end
		end
	end
end

--中毒buff加深
function OnCrossEatChickenToxicBuff(fubenPtr)
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicBuff")			--中毒buff
	local fubenIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_IDX )
	local idx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_BUFF_IDX ) + 1
	--print("OnCrossEatChickenToxicBuff idx ="..idx)
	local toxicCd = CrossEatChickenCfg.toxicBuffCd[idx + 1]
	if not toxicCd then
		toxicCd = CrossEatChickenCfg.toxicBuffCd[#CrossEatChickenCfg.toxicBuffCd]
	end
	--print("toxicCd ="..toxicCd)
	Fuben.regScriptTimer2S(fubenPtr, 0, toxicCd * 1000, toxicCd * 1000, -1, "OnCrossEatChickenToxicBuff")

	local dVar = System.getDyanmicVar()
	if not dVar.crossEatChickenToxic then
		dVar.crossEatChickenToxic = {}
	end
	if not dVar.crossEatChickenToxic[fubenIdx] then
		dVar.crossEatChickenToxic[fubenIdx] = {}
	end

	local buffId = CrossEatChickenCfg.toxicBuffId[idx + 1]
	if not buffId then
		buffId = CrossEatChickenCfg.toxicBuffId[#CrossEatChickenCfg.toxicBuffId]
	end

	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for ii, sysarg in ipairs(playerList) do
		local value = Actor.getMapAttriValue(sysarg, aaRunNpc)
		if #dVar.crossEatChickenToxic[fubenIdx] > 0 then
			for iii = 1, #dVar.crossEatChickenToxic[fubenIdx] do
				if value == dVar.crossEatChickenToxic[fubenIdx][iii] then
					if not Actor.hasBuffById(sysarg, buffId) then
						Actor.addBuffById(sysarg, buffId)
					end
				end
			end
		end
	end
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_BUFF_IDX, idx)
end

--掉物品
function OnCrossEatChickenDropItem(fubenPtr)
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropItem")			--掉补给
	local idx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_GIFT_IDX ) + 1
	--print("OnCrossEatChickenDropItem idx ="..idx)
	local giftCd = CrossEatChickenCfg.giftMonsterCd[idx]
	if not giftCd then
		giftCd = CrossEatChickenCfg.giftMonsterCd[#CrossEatChickenCfg.giftMonsterCd]
	end
	--print("giftCd ="..giftCd)
	Fuben.regScriptTimer2S(fubenPtr, 0, giftCd * 1000, giftCd * 1000, -1, "OnCrossEatChickenDropItem")
	local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	
	--print("OnCrossEatChickenDropItem idx ="..idx)
	for ii = 1, #CrossEatChickenCfg.Range do
		local cfg = CrossEatChickenCfg.Range[ii]
		if cfg and cfg.rangeMonsters then
			local config = cfg.rangeMonsters[idx]
			if not config then
				config = cfg.rangeMonsters[#cfg.rangeMonsters]
			end
			if config then
				for i, v in ipairs(config) do
					freshSceneMonsterInRangeForce(v, fubenHandle)
				end
			end
		end
	end
	local hScene = Fuben.getSceneHandleById(CrossEatChickenCfg.sceneId, fubenHandle)
	Fuben.sendMsgToScene(hScene,string.format(Lang.ScriptTips.CrossEatChicken018, idx), ttScreenCenter + ttChatWindow)
	--[[
	local randNum = System.getRandomNumber(#CrossEatChickenCfg.Range) + 1
	local exCfg = CrossEatChickenCfg.Range[randNum]
	if exCfg and exCfg.exRangeMonsters then
		local exConfig = exCfg.exRangeMonsters[idx]
		if not exConfig then
				exConfig = exCfg.exRangeMonsters[#exCfg.exRangeMonsters]
			end
		if exConfig then
			for i, v in ipairs(exConfig) do
				freshSceneMonsterInRangeForce(v, fubenHandle)
			end
		end
		Fuben.sendMsgToScene(hScene,string.format(Lang.ScriptTips.CrossEatChicken019, idx, exCfg.rangeName), ttScreenCenter + ttChatWindow)
	end
	]]
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_GIFT_IDX, idx)
end


--掉物品
function OnCrossEatChickenDropExItem(fubenPtr)
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropExItem")			--掉额外补给
	local idx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_EXGIFT_IDX ) + 1
	--print("OnCrossEatChickenDropExItem idx ="..idx)
	local exGiftCd = CrossEatChickenCfg.exGiftMonsterCd[idx + 1]
	if not exGiftCd then
		exGiftCd = CrossEatChickenCfg.exGiftMonsterCd[#CrossEatChickenCfg.exGiftMonsterCd]
	end
	--print("exGiftCd ="..exGiftCd)
	Fuben.regScriptTimer2S(fubenPtr, 0, exGiftCd * 1000, exGiftCd * 1000, -1, "OnCrossEatChickenDropExItem")
	local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	
	--print("OnCrossEatChickenDropExItem idx ="..idx)
	
	local hScene = Fuben.getSceneHandleById(CrossEatChickenCfg.sceneId, fubenHandle)
	local randNum = System.getRandomNumber(#CrossEatChickenCfg.Range) + 1
	local exCfg = CrossEatChickenCfg.Range[randNum]
	if exCfg and exCfg.exRangeMonsters then
		local exConfig = exCfg.exRangeMonsters[idx]
		if not exConfig then
			exConfig = exCfg.exRangeMonsters[#exCfg.exRangeMonsters]
		end
		if exConfig then
			for i, v in ipairs(exConfig) do
				freshSceneMonsterInRangeForce(v, fubenHandle)
			end
		end
		Fuben.sendMsgToScene(hScene,string.format(Lang.ScriptTips.CrossEatChicken019, idx, exCfg.rangeName), ttScreenCenter + ttChatWindow)
	end
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_EXGIFT_IDX, idx)
end

--刷爆炸怪
function OnCrossEatChickenExplode(fubenPtr)
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenExplode")				--自爆怪
	local idx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_IDX ) + 1
	--print("OnCrossEatChickenExplode idx ="..idx)
	local explodeCd = CrossEatChickenCfg.explodeMonstersCd[idx + 1]
	if not explodeCd then
		explodeCd = CrossEatChickenCfg.explodeMonstersCd[#CrossEatChickenCfg.explodeMonstersCd]
	end
	--print("explodeCd ="..explodeCd)
	Fuben.regScriptTimer2S(fubenPtr, 0, explodeCd * 1000, explodeCd * 1000, -1, "OnCrossEatChickenExplode")

	local fubenHandle = Fuben.getFubenHandle(fubenPtr)
	
	--print("OnCrossEatChickenExplode idx ="..idx)
	local randNum = System.getRandomNumber(#CrossEatChickenCfg.Range) + 1
	local cfg = CrossEatChickenCfg.Range[randNum]
	if cfg and cfg.explodeMonsters then
		local config = cfg.explodeMonsters[idx]
		if not config then
			config = cfg.explodeMonsters[#cfg.explodeMonsters]
		end
		if config then
			for i, v in ipairs(config) do
				freshSceneMonsterInRangeForce(v, fubenHandle)
			end
		end
	end
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_IDX, idx )

	local time = System.getCurrMiniTime()
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_TIME, time +  explodeCd)

	ChangeCrossEatChickenWar(fubenPtr)
end

--毒区增加
function OnCrossEatChickenToxicArea(fubenPtr)
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicArea")			--毒圈

	local fubenIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_IDX )
	local idx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_IDX ) + 1
	--print("OnCrossEatChickenToxicArea idx ="..idx)
	local toxicRangeCd = CrossEatChickenCfg.toxicRangeCd[idx]
	if not toxicRangeCd then
		toxicRangeCd = CrossEatChickenCfg.toxicRangeCd[#CrossEatChickenCfg.toxicRangeCd]
	end
	--print("toxicRangeCd ="..toxicRangeCd)
	Fuben.regScriptTimer2S(fubenPtr, 0, toxicRangeCd * 1000, toxicRangeCd * 1000, -1, "OnCrossEatChickenToxicArea")
	
	--print("OnCrossEatChickenToxicArea idx ="..idx)
	local dVar = System.getDyanmicVar()
	if not dVar.crossEatChickenToxic then
		dVar.crossEatChickenToxic = {}
	end
	if not dVar.crossEatChickenToxic[fubenIdx] then
		dVar.crossEatChickenToxic[fubenIdx] = {}
	end
	if idx ~= 1 and #dVar.crossEatChickenToxic[fubenIdx] < CrossEatChickenCfg.maxToxicRangeNum then
		local nextId = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_NEXT )
		if nextId > 0 and nextId <= #CrossEatChickenCfg.Range then
			--print("nextId = "..nextId)
			dVar.crossEatChickenToxic[fubenIdx][#dVar.crossEatChickenToxic[fubenIdx] + 1] = nextId

			local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
			for i, v in ipairs(playerList) do
				NoticeCrossEatChickenToxic(v, dVar.crossEatChickenToxic[fubenIdx])
			end
			local buffIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_BUFF_IDX ) + 1
			local buffId = CrossEatChickenCfg.toxicBuffId[buffIdx]
			if not buffId then
				buffId = CrossEatChickenCfg.toxicBuffId[#CrossEatChickenCfg.toxicBuffId]
			end
			local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
			for iii, sysarg in ipairs(playerList) do
				local value = Actor.getMapAttriValue(sysarg, aaRunNpc)
				--print("value ="..(value or 0))
				if value == nextId then
					if not Actor.hasBuffById(sysarg, buffId) then
						Actor.addBuffById(sysarg, buffId)
					end
				end
			end
		end
	end
	if #dVar.crossEatChickenToxic[fubenIdx] >= CrossEatChickenCfg.maxToxicRangeNum then
		Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_NEXT, -1 )
		Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_TIME, -1 )
	else
		local ii, cfg = GetCrossEatChickenIdxRand(CrossEatChickenCfg.Range, dVar.crossEatChickenToxic[fubenIdx])
		--print("OnCrossEatChickenToxicArea ii = "..(ii or 0))
		if cfg then
			Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_NEXT, cfg.rangeId )
			Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_TIME, toxicRangeCd + System.getCurrMiniTime() )
		end
	end
	Fuben.setFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_IDX, idx)
	
	ChangeCrossEatChickenWar(fubenPtr)
end
	
--手动离开
function CrossEatChickenLeaveActivity(sysarg)
	if not IsInCrossEatChickenFuben(sysarg) then
		return
	end
	local fubenPtr = Actor.getFubenPrt( sysarg )

	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	local rewardIndex = #playerList
	local cfg = GetElemByCond( CrossEatChickenCfg.pkScore,  rewardIndex)
	if cfg and cfg.addScore then
		CrossEatChickenAddScore(sysarg, rewardIndex, cfg.addScore)
	end
	if cfg and cfg.award and IsBagGridEnough(sysarg, cfg.award) then
		GiveCommonAward(sysarg, cfg.award, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken)
	end
	Actor.exitFuben( sysarg )
	ChangeCrossEatChickenWar(fubenPtr)
end

--死亡
function CrossEatChickenActorDeath( sysarg, killerPtr )
	if not IsInCrossEatChickenFuben(sysarg) then
		return
	end
	local fubenPtr = Actor.getFubenPrt( sysarg )
	--[[
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	
	local rewardIndex = #playerList
	local cfg = GetElemByCond( CrossEatChickenCfg.pkScore,  rewardIndex)
	if cfg and cfg.addScore then
		CrossEatChickenAddScore(sysarg, rewardIndex, cfg.addScore)
	end
	Actor.returnCity( sysarg )
	]]
	if killerPtr and Actor.getEntityType(killerPtr) == 0 then
		local killerId = Actor.getActorId(killerPtr)
		--print("killerId = "..killerId)
		local data = gRankMgr_GetValueById(killerId, RANK_CROSS_EAT_CHICKEN_RANK, 1)
		local myData = BaseFuc_unserialize(data)

		if not myData then
			myData = {}
		end
		
		if not myData.name then
			myData.name = Actor.getName(killerPtr)
			myData.level = Actor.getIntProperty(killerPtr, PROP_CREATURE_LEVEL)
			myData.power = Actor.getIntProperty(killerPtr, PROP_ACTOR_BATTLE_POWER)
		end
		if not myData.sex then
	        myData.sex = Actor.getIntProperty(killerPtr, PROP_ACTOR_SEX)
	        myData.vocation = Actor.getIntProperty(killerPtr, PROP_ACTOR_VOCATION)
	    end
		if not myData.dailyCount then
			myData.dailyCount = 0
		end
		if not myData.myKillNumber then
			myData.myKillNumber = 0
		end
		myData.myKillNumber = myData.myKillNumber + 1
		local newData = BaseFuc_serialize(myData)
		--print("newData ="..newData)
		gRankMgr_SetRankEx(killerId, RANK_CROSS_EAT_CHICKEN_RANK, 1, newData)
	end
	ChangeCrossEatChickenWar(fubenPtr)
end

--离开场景
function CrossEatChickenLeaveActivityHandler(sysarg)
	CloseActivityRightPanel(sysarg)
	ClearCrossEatChickenBuff(sysarg)
	ActorHpMpInCrossEatChicken(sysarg, true)
	ClearCrossEatChickenBagItem(sysarg)
end

--[[
副本时间到了
]]
function CrossEatChickenTimeout( fubenPtr )
	local fubenId = Fuben.getFuBenId(fubenPtr)
	if fubenId ~= CrossEatChickenCfg.fubenId then
		return
	end 

	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	local len  = #playerList
	
	for i, player in ipairs( playerList or {} ) do
		local cfg = GetElemByCond( CrossEatChickenCfg.pkScore,  len)
		if cfg and cfg.addScore then
			CrossEatChickenAddScore(player, len, cfg.addScore)
		end
		if cfg and cfg.award and IsBagGridEnough(player, cfg.award) then
			GiveCommonAward(player, cfg.award, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken)
		end
		Actor.exitFuben(player)
	end

	local dVar = System.getDyanmicVar()
	if not dVar.crossEatChicken then
		dVar.crossEatChicken = {}
	end

	local crossEatIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_IDX ) or 0
	if crossEatIdx > 0 then
		dVar.crossEatChicken[crossEatIdx] = nil
	end

	if not dVar.crossEatChickenToxic then
		dVar.crossEatChickenToxic = {}
	end
	dVar.crossEatChickenToxic[crossEatIdx] = nil

	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropItem")			--掉补给
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenExplode")				--自爆怪
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicArea")			--毒圈
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicBuff")			--中毒buff
	Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropExItem")			--掉补给
end

--积分增加
function CrossEatChickenAddScore(sysarg, rewardIndex, addScore)
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_CROSS_EAT_CHICKEN_RANK, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData then
		myData = {}
	end
	
	if not myData.name then
		myData.name = Actor.getName(sysarg)
		myData.level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
		myData.power = Actor.getIntProperty(sysarg, PROP_ACTOR_BATTLE_POWER)
	end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end
    
    if not myData.myKillNumber then
		myData.myKillNumber = 0
	end

	gRankMgr_AddValue(actorid, RANK_CROSS_EAT_CHICKEN_RANK, addScore)

	SendCrossEatChickenResult(sysarg, rewardIndex, myData.myKillNumber, addScore)

	if myData.dailyCount and myData.dailyCount >= CrossEatChickenCfg.dailyCount then
		return
	end

	local mySignState
	if not IsCrossEatChickenCanSign() then
		mySignState = 0
	else
		mySignState = 1
	end
	SendCrossEatChickenSignStateChange(sysarg, mySignState, 1)
end

--左侧面板初始化
function OpenCrossEatChickenPanel( sysarg )
	if not IsInCrossEatChickenFuben( sysarg ) then
		--print("OpenCrossEatChickenPanel not IsInCrossEatChickenFuben")
		return
	end
	local fubenPtr = Actor.getFubenPrt(sysarg)

	local activityType 	= 1 			--活动
	local activityId 	= enRightPanelActivity_CrossEatChicken
	local restTime 		= Fuben.getFbTime( fubenPtr )
	if restTime < 0 then
		restTime = 0
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	panelInfo.title 	= Lang.ScriptTips.CrossEatChickenPanel001
	panelInfo.timeTitle = Lang.ScriptTips.CrossEatChickenPanel002			--剩余时间
	panelInfo.restTime	= restTime

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )

	local time = System.getCurrMiniTime()
	local nextRange = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_NEXT )
	if nextRange == 0 then
		nextRange = 1
	end
	local nextRangeToxicTime = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_TIME )
	local ToxicIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_IDX )
	if nextRangeToxicTime == 0 then
		nextRangeToxicTime = CrossEatChickenCfg.toxicRangeCd[ToxicIdx] + time
	end

	local lastTime = nextRangeToxicTime - time
	if nextRange == -1 then
		nextRange = 0
		lastTime = 0
	end
	local explodeIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_IDX )
	local explodeTime = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_TIME )
	if explodeTime == 0 then
		explodeTime = CrossEatChickenCfg.explodeMonstersCd[explodeIdx + 1] + time
	end
	local explodeLastTime = explodeTime - time
	local sVar = System.getStaticVar()
	local startNum = sVar.szEatChickenStartCount or CrossEatChickenCfg.openGameCount
	NoticeCrossEatChickenWar(sysarg, 0, startNum, nextRange, lastTime, explodeLastTime)
end

--通知数据改变
function ChangeCrossEatChickenWar(fubenPtr)
	local playerList = LuaHelp.getFbActorList(fubenPtr)
	local playerNum = 0
	if playerList then
		playerNum = #playerList
		if playerNum == 1 then
			local dVar = System.getDyanmicVar()
			if not dVar.crossEatChicken then
				dVar.crossEatChicken = {}
			end

			local crossEatIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_IDX ) or 0
			if crossEatIdx > 0 then
				dVar.crossEatChicken[crossEatIdx] = nil
			end

			if not dVar.crossEatChickenToxic then
				dVar.crossEatChickenToxic = {}
			end
			dVar.crossEatChickenToxic[crossEatIdx] = nil

			Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropItem")			--掉补给
			Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenDropExItem")			--特殊掉补给
			Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenExplode")				--自爆怪
			Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicArea")			--毒圈
			Fuben.unregScriptCallback(fubenPtr, "OnCrossEatChickenToxicBuff")			--中毒buff

			for i, sysarg in ipairs(playerList) do
				if i == 1 then
					local cfg = GetElemByCond( CrossEatChickenCfg.pkScore,  i)
					local addScore = 0
					if cfg and cfg.addScore then
						CrossEatChickenAddScore(sysarg, i, cfg.addScore)
						addScore = cfg.addScore
					end
					if cfg and cfg.award and IsBagGridEnough(sysarg, cfg.award) then
						GiveCommonAward(sysarg, cfg.award, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken)
					end
					Actor.exitFuben( sysarg )
					local actorName = Actor.getName(sysarg)
					
					System.broadcastTipmsg(string.format(Lang.ScriptTips.CrossEatChicken021, actorName, addScore),ttScreenCenter + ttChatWindow)
					break
				end
			end

			local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
			Fuben.setFubenTime( fubenHandle, 1 )
		else
			for i, sysarg in ipairs(playerList) do
				if not IsInCrossEatChickenFuben( sysarg ) then
					return
				end
				local fubenPtr = Actor.getFubenPrt(sysarg)
				local actorId = Actor.getActorId(sysarg)
				--print("actorId = "..actorId)
				local data = gRankMgr_GetValueById(actorId, RANK_CROSS_EAT_CHICKEN_RANK, 1)
				--print("data ="..(data or 0))
				local myData = BaseFuc_unserialize(data)

				if not myData then
					myData = {}
				end
				
				if not myData.myKillNumber then
					myData.myKillNumber = 0
				end

				--print("myData.myKillNumber ="..myData.myKillNumber)

				local time = System.getCurrMiniTime()
				local nextRange = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_NEXT )
				if nextRange == 0 then
					nextRange = 1
				end
				local nextRangeToxicTime = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_TIME )
				local ToxicIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_TOXIC_IDX )
				if nextRangeToxicTime == 0 then
					nextRangeToxicTime = CrossEatChickenCfg.toxicRangeCd[ToxicIdx] + time
				end

				local lastTime = nextRangeToxicTime - time
				if nextRange == -1 then
					nextRange = 0
					lastTime = 0
				end
				local explodeIdx = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_IDX )
				local explodeTime = Fuben.getFbValue( fubenPtr, CROSS_EATCHICKEN_EXPLODE_TIME )
				if explodeTime == 0 then
					explodeTime = CrossEatChickenCfg.explodeMonstersCd[explodeIdx + 1] + time
				end
				local explodeLastTime = explodeTime - time
				NoticeCrossEatChickenWar(sysarg, myData.myKillNumber, playerNum, nextRange, lastTime, explodeLastTime)
			end
		end
	end
end

--获取下一个随机区域
function GetCrossEatChickenIdxRand(Range, data)
	local dataTable = {}
	if data then
		for idx = 1, #data do
			--print("data[idx] ="..data[idx])
			dataTable[data[idx]] = 1
		end
	end
	local totalWeight = 0
	for i, cfg in ipairs(Range) do
		if cfg and cfg.rangeId and not dataTable[cfg.rangeId] then
			totalWeight = totalWeight + (cfg.weight or 0)
		end
	end
	--print("totalWeight ="..totalWeight)
	local ranNum = System.getRandomNumber(totalWeight)
	--print("ranNum ="..ranNum)
	local weightValue = 0
	for i, cfg in ipairs(Range) do
		if cfg and cfg.rangeId and not dataTable[cfg.rangeId] then
			if weightValue <= ranNum and ranNum <= weightValue + (cfg.weight or 0) then
				return cfg.rangeId, cfg
			end
			weightValue = weightValue + (cfg.weight or 0)
		end
	end
	return Range[#Range].rangeId, Range[#Range]
end


--Hp,Mp处理
function  ActorHpMpInCrossEatChicken(sysarg,boRecover)
    local ActVar = Actor.getDyanmicVar(sysarg)
    if boRecover then
		--恢复hp,mp
		local hp = ActVar.EnterCrossEatChickenHP or 0
		local mp = ActVar.EnterCrossEatChickenMP or 0
		if hp > 0 then
		    Actor.setUIntProperty(sysarg,PROP_CREATURE_HP,hp)
		end
		if mp > 0 then
		    Actor.setUIntProperty(sysarg,PROP_CREATURE_MP,mp)
		end
	else
	   Actor.collectOperate(sysarg,3)
	end
end

--离开场景清空物品
function ClearCrossEatChickenBagItem(sysarg)
	for ii, item in ipairs(CrossEatChickenCfg.items) do
    	if item and item.id then
    		RemoveActorItemInBag( sysarg, item.id, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken )
    	end
    end
    for ii, item in ipairs(CrossEatChickenCfg.clientItems) do
    	if item and item.id then
    		RemoveActorItemInBag( sysarg, item.id, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken )
    	end
    end
    for ii, itemId in ipairs(CrossEatChickenCfg.exItems) do
    	if itemId then
    		RemoveActorItemInBag( sysarg, itemId, LogIds.CrossEatChickenLogId, Lang.LogTips.CrossEatChicken )
    	end
    end

    Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossEatChicken017, ttFlyTip)
end

--离开清除buff
function ClearCrossEatChickenBuff(sysarg)
	local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    if CrossEatChickenCfg.enterSceneBuffList[vocation] then
    	for i, v in ipairs(CrossEatChickenCfg.enterSceneBuffList[vocation]) do
    		if Actor.hasBuffById(sysarg, v) then
    			Actor.delBuffById(sysarg, v)
    		end
    	end
    end
    for ii, item in ipairs(CrossEatChickenCfg.items) do
    	if item and item.buffList then
    		for i,v in ipairs(item.buffList) do
        		if Actor.hasBuffById(sysarg, v) then
        			Actor.delBuffById(sysarg, v)
        		end
        	end
    	end
    end
    for iii, buffId in ipairs(CrossEatChickenCfg.toxicBuffId) do
    	if Actor.hasBuffById(sysarg, buffId) then
			Actor.delBuffById(sysarg, buffId)
		end
    end
    if CrossEatChickenCfg.eatChickenFationBuff and Actor.hasBuffById(sysarg, CrossEatChickenCfg.eatChickenFationBuff) then
		Actor.delBuffById(sysarg, CrossEatChickenCfg.eatChickenFationBuff)
	end
	Actor.collectOperate(sysarg,0)
end

--进入场景加buff
function AddCrossEatChickenBuff(sysarg) 
	local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    if CrossEatChickenCfg.enterSceneBuffList and CrossEatChickenCfg.enterSceneBuffList[vocation] then
    	for i, v in ipairs(CrossEatChickenCfg.enterSceneBuffList[vocation]) do
    		if not Actor.hasBuffById(sysarg, v) then
    			Actor.addBuffById(sysarg, v)
    		end
    	end
    end
    local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
    local fation = Actor.getIntProperty(sysarg, PROP_ACTOR_FATION_MALE)
    if sex == 1 then
        fation = Actor.getIntProperty(sysarg, PROP_ACTOR_FATION_FEMALE)
    end
    if CrossEatChickenCfg.eatChickenFationBuff and System.getIntBit(fation, CrossEatChickenCfg.eatChickenFationId-1) == 1 and not Actor.hasBuffById(sysarg, CrossEatChickenCfg.eatChickenFationBuff) then
        Actor.addBuffById(sysarg, CrossEatChickenCfg.eatChickenFationBuff)
    end
	Actor.collectOperate(sysarg,0)	
end

--失去物品
function CrossEatChickenDropItem( sysarg, itemId, itemCount )
    if not IsCrossEatChickenStart() then
        return 
    end

    if not IsInCrossEatChickenFuben(sysarg) then
        return
    end
    if itemCount > 0 then
        local num = Actor.getItemCount(sysarg, itemId)
        if num > 0 then
            return
        end
        local cfg = GetCrossEatChickenItemConfigById(itemId)
        if cfg then
        	for i,v in ipairs(cfg.buffList) do
        		if Actor.hasBuffById(sysarg, v) then
        			Actor.delBuffById(sysarg, v)
        		end
        	end
        	Actor.collectOperate(sysarg,0)	
		end
    end
end

--获得物品
function CrossEatChickenPickItem( sysarg, itemId, itemCount )
    if IsInCrossEatChickenFuben(sysarg) and IsCrossEatChickenStart() then
        if itemCount > 0 then
	        local num = Actor.getItemCount(sysarg, itemId)
	        if num > 0 then
	           	local cfg = GetCrossEatChickenItemConfigById(itemId)
		        if cfg then
		        	for i,v in ipairs(cfg.buffList) do
		        		if not Actor.hasBuffById(sysarg, v) then
		        			Actor.addBuffById(sysarg, v)
		        		end
		        	end
		        	Actor.collectOperate(sysarg,0)	
				end 
	        end
	    end
    end
end

--获取物品配置
function GetCrossEatChickenItemConfigById(itemId)
	for i , v in ipairs(CrossEatChickenCfg.items) do
		if v.id == itemId then
			return v
		end
	end
end

--gm
function gmCrossEatChicken(sysarg, opType, data)
	if opType == 1 then
		CrossEatChickenClear()
	elseif opType == 2 then
		CrossEatChickenSignBroad()
	elseif opType == 3 then
		CrossEatChickenSignClose()
	elseif opType == 4 then
		CrossEatChickenCalc()
	elseif opType == 5 then
		SendCrossEatChickenDataSub(sysarg)
	elseif opType == 6 then
		CrossEatChickenSignSub(sysarg, data)
	elseif opType == 7 then
		CrossEatChickenSettleSub(sysarg, data)
	elseif opType == 8 then
		CrossEatChickenGetAwardSub(sysarg)
	elseif opType == 9 then
		CrossEatChickenResetCount()
	end
end